package lihua.robot.controller;

import cn.hutool.core.date.DateUtil;
import lihua.robot.common.Result;
import lihua.robot.service.FallingTimeService;
import lihua.robot.utils.PostManUtil;
import lihua.robot.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static lihua.robot.utils.RedisUtil.getWarehouselocation;
import static lihua.robot.utils.TransportationConditionsUtil.determineTheHandoverArea;
import static lihua.robot.utils.TransportationConditionsUtil.setKW0;

@RestController
@CrossOrigin
@RequestMapping(value="/robot_actions")
public class BlocksController {


    @Autowired
    private FallingTimeService fallingTimeService;

    /**
     * 大桶运输流程1
     * 1、AGV小车到对应AP点位（即细砂机）搬桶
     * 2、再到满桶存放处卸桶
     */

    @GetMapping("/transport_full_barrels")
    public Result transportFullBarrels(@RequestParam("deSvn") String deSvn,
                                       @RequestParam("MAP") String MAP) {
        String name = "AMB-0301";
        String AP = "AP" + deSvn;
        //定义订单id
        String id = DateUtil.date() + "-B1-" + AP;

        //定义订单动作
        List<Map<String, Object>> blocks = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("blockId", "b1");
        map.put("location", AP);
        map.put("operation", "JackLoad");
        blocks.add(map);

        Map<String, Object> map1 = new HashMap<>();
        map1.put("blockId", "b2");
        map1.put("location", MAP);
        map1.put("operation", "JackUnload");
        blocks.add(map1);

        PostManUtil.sendBlocksPost(id, blocks, name);
        System.out.println("调用成功" + id);
        return Result.success(id, "调用成功");
    }

    /**
     * 大桶运输流程2
     * 1、AGV小车到空桶位搬桶
     * 2、到对应细纱设备卸桶
     */

    @GetMapping("/transport_empty_drums")
    public Result transportEmptyDrums(@RequestParam("deSvn") String deSvn,
                                      @RequestParam("KAP") String KAP) {
        String name = "AMB-0301";
        String AP = "AP" + deSvn;
        //定义订单id
        String id = DateUtil.date() + "-B2-" + AP;

        //定义订单动作
        List<Map<String, Object>> blocks = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("blockId", "b1");
        map.put("location", KAP);
        map.put("operation", "JackLoad");
        blocks.add(map);

        Map<String, Object> map1 = new HashMap<>();
        map1.put("blockId", "b2");
        map1.put("location", AP);
        map1.put("operation", "JackUnload");
        blocks.add(map1);

        PostManUtil.sendBlocksPost(id, blocks, name);
        System.out.println("调用成功" + id);
        return Result.success(id, "调用成功");
    }

    /**
     * 用于测试小桶的主流程
     * 1、机器人先到小桶满桶点位搬货
     * 2、再到指定AP点位卸货
     */

    @GetMapping("/s_transport_full_barrels")
    public Result s_transportFullBarrels(@RequestParam("deSvn") Integer deSvn,
                                         @RequestParam("XMAP") String XMAP) {

        String name = "AMB-0302";

        String AP = "AP" + (400 + deSvn);
        //定义订单id
        String id = DateUtil.date() + "-small1-" + AP;

        //定义订单动作
        List<Map<String, Object>> blocks = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("blockId", "b1");
        map.put("location", XMAP);
        map.put("operation", "JackLoad");
        blocks.add(map);

        Map<String, Object> map1 = new HashMap<>();
        map1.put("blockId", "b2");
        map1.put("location", AP);
        map1.put("operation", "JackUnload");
        blocks.add(map1);

        PostManUtil.sendBlocksPost(id, blocks, name);

        return Result.success(id, "调用成功");
    }


    /**
     * 用于测试小桶的主流程
     * 3、指定AP点位搬空桶
     * 4、回到小桶空桶点位卸货
     */

    @GetMapping("/s_transport_empty_drums")
    public Result s_transportEmptyDrums(@RequestParam("deSvn") Integer deSvn,
                                        @RequestParam("XKAP") String XKAP) {

        String name = "AMB-0302";

        String AP = "AP" + (400 + deSvn);
        //定义订单id
        String id = DateUtil.date() + "-small2-" + AP;

        //定义订单动作
        List<Map<String, Object>> blocks = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("blockId", "b1");
        map1.put("location", AP);
        map1.put("operation", "JackLoad");
        blocks.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("blockId", "b2");
        map2.put("location", XKAP);
        map2.put("operation", "JackUnload");
        blocks.add(map2);

        PostManUtil.sendBlocksPost(id, blocks, name);

        return Result.success(id, "调用成功");
    }


    /**
     * 用于手动控制机器人
     */

    @GetMapping("/manual_action")
    public Result ManualAction(@RequestParam("deSvn") Integer deSvn,
                               @RequestParam("MAP") String MAP,
                               @RequestParam("KAP") String KAP,
                               @RequestParam("XKAP") String XKAP,
                               @RequestParam("XMAP") String XMAP) {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(4);
        BlocksController blocksController = new BlocksController();
        int devsn = deSvn;
        if(XMAP != null){
            executorService.schedule(() -> {
                blocksController.s_transportFullBarrels(devsn, XMAP);
            }, 1, TimeUnit.MINUTES);
        }

        if(MAP != null) {
            // 大桶运单1
            executorService.schedule(() -> {
                blocksController.transportFullBarrels(String.valueOf(devsn), MAP);
            }, 2, TimeUnit.MINUTES);
        }


        if(KAP != null) {
            // 大桶运单2
            executorService.schedule(() -> {
                blocksController.transportEmptyDrums(String.valueOf(devsn), KAP);
            }, 15, TimeUnit.MINUTES);
        }


        if(XKAP != null) {
            //小桶运单2
            executorService.schedule(() -> {
                blocksController.s_transportEmptyDrums(devsn, XKAP);
            }, 20, TimeUnit.MINUTES);
        }
        return Result.success("手动执行机器人成功");
    }

}
